<?php

function diff_element($arr1,$arr2){

    $result = array();
    
    foreach($arr1 as $key => $val ){
    
    if(!in_array($val, $arr2))
    
    array_push($result, $val);
    
    }
    
    foreach($arr2 as $key => $val ){
    
    if(!in_array($val, $arr1))
    
    array_push($result, $val);
    
    }
    
    return $result;
    
    }
    
function array_unique_fb($array2D=[]) {
$temp = [];
foreach ($array2D as $v) {
    $v = join(",", $v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
    $temp[] = $v;
}
$temp = array_unique($temp);//去掉重复的字符串,也就是重复的一维数组
foreach ($temp as $k => $v) {
    $temp[$k] = explode(",", $v);//再将拆开的数组重新组装
}
return $temp;
}

/*打印数组*/
function p($star){
    echo '<pre style="background: rgba(255,204,204,.5);color: #FF6633;">';
    print_r($star);
    echo '</pre>';
    //exit;
}
/**

  * 计算时间差

  * @param int $timestamp1 时间戳开始

  * @param int $timestamp2 时间戳结束

  * @return array

  */
 function time_diff($timestamp1, $timestamp2)
 {
     if ($timestamp2 <= $timestamp1)
     {
         return ['hours'=>0, 'minutes'=>0, 'seconds'=>0];
     }
     $timediff = $timestamp2 - $timestamp1;
     // 时
     $remain = $timediff%86400;
     $hours = intval(($remain/3600));
     
     // 分
     $remain = $timediff%3600;
     $mins = intval(($remain/60));
     // 秒
    //  $secs = round(( $remain%60),2);
     $time = ['hours'=>$hours, 'minutes'=>$mins];
     return $time;
 }
    
/*$data
 * 周几
 */
function weekDate($data){
    $weekarray=array("日","一","二","三","四","五","六");
    return '星期'.$weekarray[date("w",strtotime($data))];
}


/**
 * [jumpPage 跳转函数]
 */
function jumpPage($url){
     echo '<script>location.href="'.$url.'";</script>';
}
/**
 * [jump 跳转函数]
 * @param  [string] $msg [提示的信息]
 * @param  string $url [跳转地址]
 * @return [type]      [description]
 */
function jump($msg,$url=''){
    if($url!=''){
        echo '<script>alert("'.$msg.'");location.href="'.$url.'";</script>';
    }else{
        echo '<script>alert("'.$msg.'");history.go(-1);</script>';
    }
    exit;
}
/**
 * [jump 跳转函数]
 * @$code  [string] $msg [提示的信息]
 * @$msg  string $url [跳转地址]
 * @$data [type]      [description]
 */
function jsonData($code,$msg,$data=null){
    if($data!=''){
        return json_encode(compact('code','msg','data'));
    }else{
        return json_encode(compact('code','msg'));
    }
    
   
}

//生成订单号函数
function makeSerial()
{
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn =
            $yCode[intval(date('Y')) - 2023] . date(
                'd') . substr(time(), -1) . substr(microtime(), 2, 2) . sprintf(
                '%02d', rand(0, 99));
        return $orderSn;
}


function time2string($second,$type){

	//返回字符串
	if($type){
	    return date('Y-m-d H:i:s',$second).'已处理完成';
	}else{
	    $day = floor($second/(3600*24));
        // 	$second = $second%(3600*24);//除去整天之后剩余的时间
        $hour = floor($second/3600);
        $second = $second%3600;//除去整小时之后剩余的时间 
        $minute = floor($second/60);
        if(strlen($minute)==1){
        $minute = '0'.$minute;
        }
        $second = $second%60;//除去整分钟之后剩余的时间 
        if(strlen($second)==1){
        $second = '0'.$second;
        }
	    return ($hour).'小时'.($minute).'分钟'.($second);
	   // return $hour.'小时'.$minute.'分钟'.$second;
	}
// 	return $day.'天'.$hour.'小时'.$minute.'分'.$second.'秒';
	

}

function timehour($second){

  $day = floor($second/(3600*24));
        // 	$second = $second%(3600*24);//除去整天之后剩余的时间
        $hour = floor($second/3600);
        $second = $second%3600;//除去整小时之后剩余的时间 
        $minute = floor($second/60);
        if(strlen($minute)==1){
        $minute = '0'.$minute;
        }
        $second = $second%60;//除去整分钟之后剩余的时间 
        if(strlen($second)==1){
        $second = '0'.$second;
        }
	    return $hour.'小时'.$minute.'分钟'.$second;

	

}
/**
 * $TimeData 时间选择返回数据
 */
function timadatas($TimeData)
{
            $tr_name = explode("-",$TimeData);
    
            $tr_name = array_map("trim",$tr_name);
            $time1 = $tr_name[0]."-".$tr_name[1]."-".$tr_name[2].'00:00:00';
            $time2 = $tr_name[3]."-".$tr_name[4]."-".$tr_name[5].'23:59:59';
            $time1 = strtotime($time1);
            $time2 = strtotime($time2);
            return  $data[]=['time1'=>$time1,'time2'=>$time2];
       
}
function str_repl($TimeData)
{
    $str = str_replace('[', '',$TimeData);
    $str = str_replace(']', '',$str);
    $str = str_replace('“', '',$str);
    $str = str_replace('”', '',$str);
    return $str;
       
}

//生成订单号函数
function makeOrderNo()
{
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn =
            $yCode[intval(date('Y')) - 2019] . strtoupper(dechex(date('m'))) . date(
                'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
                '%02d', rand(0, 99));
        return $orderSn;
}

function strposdatas($string)
{
  
    $start = strpos($string, '-');
    $text = substr($string, $start+1);
    $start_text = strpos($text, '-');
    echo substr($text, $start_text+1);
         
    //2
    $arr = explode('-', $string);
    unset($arr[0]);
    unset($arr[1]);
    return $string = implode('-', $arr);

}


  /**
     * @desc 获取二维数组某个键的最大值或最小值
     *
     * @param array $arr
     * @param string $keys
     * @param array $data
     */
function phpMaxMin($arr = [],$keys = ''){
        $max['key'] = '';
        $max['Qty'] = '';
        $data = [];

        // $min['key'] = '';
        // $min['value'] = '';

        foreach ($arr as $key => $val){

            if($max['key'] === ''){
                $max['key'] = $key;
                $max['Qty'] = $val[$keys];
                $data = $val;
            }

            if((int)$max['Qty'] < $val[$keys]){
                $max['key'] = $key;
                $max['Qty'] = $val[$keys];
                $data = $val;

 
            }

            // if($min['key'] === ''){

            //     $min['key'] = $key;
            //     $min['value'] = $val[$keys];

            // }

            // if((int)$min['value'] > $val[$keys]){

            //     $min['key'] = $key;
            //     $min['value'] = $val[$keys];
            // }

        }

        // $array['max'] = $max;
  
        // $array['min'] = $min;
        return $data;

    }

// 应用公共文件
function getSpApiAccessToken()
{
    $url = 'https://api.amazon.com/auth/o2/token';
    //grant_type=authorization_code&code=SplxlOexamplebYS6WxSbIA&client_id=foodev&client_secret=Y76SDl2F
    $data['grant_type'] = 'refresh_token';
    $data['refresh_token'] = '';
    $data['client_id'] = '';
    $data['client_secret'] = '';
    $res = $this->post($url, $data);
    $res = json_decode($res);
    // 永久存储refresh_token，selling_partner_id,和你后台的店铺ID绑定起来
    // 用这个值代替sdk里的refresh_token即可
    // 缓存accessToken，一般为3600秒,
    // 你来表演
    $access_token = $res['access_token'];
    return  $access_token;
}
/**
 * 数据传输加密方法
 *
 * @param array         $data       需要加密的数组
 * @param string        $key        秘钥，如果为空则使用系统默认的秘钥
 * @return boolean|string
 */
function encrypt_get_data($data, $key='')
{
    if (!$key) {
        $key = '123456';
    }
    if (!is_array($data))
    {
        return false;
    }
    $data['en_time'] = time();
    $string = json_encode($data);
    $crypttext = base64_encode(openssl_encrypt($string, 'DES-ECB', md5($key), OPENSSL_RAW_DATA));
    $encrypted = trim(safe_b64encode($crypttext));//对特殊字符进行处理

    return $encrypted;
}

/**
 * 数据传输解密方法
 *
 * @param str           $encrypted      加密的字符串
 * @param string        $key            秘钥，如果为空则使用系统默认的秘钥
 * @return string
 */
function decrypt_get_data($encryptStr, $key='')
{
    if (!$key) {
        $key = '123456';
    }
    $crypttexttb = safe_b64decode($encryptStr);//对特殊字符解析
    $decryptedtb = rtrim(openssl_decrypt($crypttexttb, 'DES-ECB', md5($key), OPENSSL_RAW_DATA), "\0");//解密函数

    return json_decode($decryptedtb, true);
}

//处理特殊字符
function safe_b64encode($string) {
    $data = base64_encode($string);
    $data = str_replace(array('+','/','='),array('-','_',''),$data);
    return $data;
}

//解析特殊字符
function safe_b64decode($string) {
    $data = str_replace(array('-','_'),array('+','/'),$string);
    $mod4 = strlen($data) % 4;
    if ($mod4) {
        $data .= substr('====', $mod4);
    }
    return base64_decode($data);
}

// 应用公共文件

    
    function curPost($url,$data)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);//不抓取头部信息。只返回数据
        curl_setopt($curl, CURLOPT_TIMEOUT,1000);//超时设置
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//1表示不返回bool值
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//重点
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        $response = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        return $response;
    }
/**
 * PHP发送Json对象数据
 * @param $url 请求url
 * @param $jsonStr 发送的json字符串
 * @return array
 */
function http_post_json($url, $jsonStr)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_POST, 1);//启用时会发送一个常规的POST请求
    curl_setopt($ch, CURLOPT_URL, $url);//需要获取的URL地址
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);//传递一个作为HTTP “POST”操作的所有数据的字符串。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);////设定是否显示头信息
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);//https请求
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr)
        )
    );
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//最后一个收到的HTTP代码
    curl_close($ch);
    return array($httpCode, $response);
}
/**
 * POST
 * @$url $url 请求url
 * @$post_data []
 */
function send_post($url,$post_data) {
    $postdata = http_build_query($post_data);
    
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => "Content-type:application/x-www-form-urlencoded",
            'content' => $postdata,
            'timeout' => 15 * 60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}


    /**
     * POST请求https接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $post [请求的参数]
     * @return  string
     */
    function post_curl($url, $post)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $res = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return json_decode($res,true); // 返回数据，json格式
 
    }

    /*
    *  php访问url路径，get请求
    */
    function curl_file_get_contents($durl){
        // header传送格式
        // $headers = array(
        //     "token:1111111111111",
        //     "over_time:22222222222",
        // );
        // 初始化
        $curl = curl_init();
        // 设置url路径
        curl_setopt($curl, CURLOPT_URL, $durl);
        // 将 curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ;
        // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
        curl_setopt($curl, CURLOPT_BINARYTRANSFER, true) ;
        // 添加头信息
        // curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        // CURLINFO_HEADER_OUT选项可以拿到请求头信息
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        // 不验证SSL
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        // 执行
        $data = curl_exec($curl);
        // 打印请求头信息
        //echo curl_getinfo($curl, CURLINFO_HEADER_OUT);
        // 关闭连接
        curl_close($curl);
        // 返回数据
        $data = json_decode($data,TRUE);
        return $data;
    }

     /**
     * CURL方式获取 
     *
     * @param unknown $server
     * @param unknown $send_data
     * @return mixed
     */
    function get_api_alimama($url){
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//加上这才能获取跳转后的
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
        //     curl_setopt($curl, CURLOPT_TIMEOUT, 5); //超时时间,自行按情况而定
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);

        //释放curl句柄
        curl_close($ch);
        return $output;
    }
if (!function_exists('exception')) {
    /**
     * 抛出异常处理
     *
     * @param string $msg 异常消息
     * @param integer $code 异常代码 默认为0
     * @param string $exception 异常类
     *
     * @throws Exception
     */
    function exception($msg, $code = 0, $exception = '')
    {
        $e = $exception ?: '\think\Exception';
        throw new $e($msg, $code);
    }
}